import { Component, OnInit } from '@angular/core';

import { TemplateListService } from './list.service';

import { Util } from '../../../common/util';

import { environment } from '../../../../environments/environment';

import * as moment from 'moment';
import * as _ from 'lodash'

@Component({
	selector: 'template-list',
	templateUrl: './list.component.html',
	styleUrls: ['./list.component.scss'],
	providers: [TemplateListService]
})
export class TemplateListComponent implements OnInit {

	public listData: any = {};

	public pageIndex = 1;

	public pageSize: any = environment.pageSize;

	public pages: any = [];

	public name: any = '';

	public selectedTemplate: any = {};

	constructor(
		private templateListService: TemplateListService,
		private util: Util
	) { }

	ngOnInit() {
		this.loadList(this.pageIndex);

	}

	editRule(item) {
		if (!item) {
			item = {};
			item.type = 'auto';
			item.isActive = false;
		}else{
        window['CKEDITOR'].instances.emailContent.setData(item.emailContent)
    window['CKEDITOR'].instances.webContent.setData(item.webContent)
    }
		this.selectedTemplate = _.clone(item);
    window['$'](".template-edit-modal").modal('show');

	}

	execRule(item) {
		let that = this;
		window['swal']({
			title: "确定?",
			text: "请确认是否要执行：" + item.name + " 指令",
			type: "warning",
			showCancelButton: true,
			// confirmButtonColor: "#DD6B55",
			confirmButtonText: "确定执行",
			cancelButtonText: "取消",
			// closeOnConfirm: true,
			// html: false
		}, function() {
			that.templateListService.execRule(item).subscribe(data => {
				if (data) {
					alert(data);
				}
			});
		});
	}
  delTemplate(item){
      let that = this;
  		window['swal']({
  			title: "确定?",
  			text: "请确认是否要删除：" + item.name + " ?",
  			type: "warning",
  			showCancelButton: true,
  			// confirmButtonColor: "#DD6B55",
  			confirmButtonText: "确定执行",
  			cancelButtonText: "取消",
  			// closeOnConfirm: true,
  			// html: false
  		}, function() {
  			that.templateListService.delTemplate(item).subscribe(data => {
  				if (data) {
  					that.loadList();
  				}
  			});
  		});
  }
	/**
	 * [loadList 加载列表]
	 * @param {[type]} index [description]
	 */
	loadList(index?) {
		let condictions: any = {};
		if (this.name) {
			condictions.name = {
				"$like": "%" + this.name + "%"
			};
		}
		this.templateListService.list({
			pageIndex: index ? index : this.pageIndex,
			pageSize: parseInt(this.pageSize),
			condictions: condictions
		}).subscribe(data => {
			if (data) {
				this.listData = data;
				console.info(data);
				this.pageIndex = this.listData.pageIndex;
				this.pages = this.util.setPage(this.listData.pageSize, this.listData.total, this.pageIndex);
			}
		});
	}

	/**
	 * [function 换页]
	 * @param {[type]} type  [description]
	 * @param {[type]} index [description]
	 */
	changePage = function(type, index) {
		var pageCount = Math.ceil(this.listData.total / this.listData.pageSize);
		if (type === 'pre') {
			if (this.pageIndex - 1 > 0) {
				this.pageIndex = this.pageIndex - 1;
				this.loadList(this.pageIndex);
			}
		} else if (type === 'next') {
			if ((this.pageIndex + 1) <= pageCount) {
				this.pageIndex = this.pageIndex + 1;
				this.loadList(this.pageIndex);
			}
		} else {
			this.pageIndex = index;
			this.loadList(this.pageIndex);
		}

	}

}
